package com.dirox.confluence.labelmanagement.action;

import com.atlassian.confluence.core.Beanable;
import com.atlassian.confluence.core.ConfluenceActionSupport;
import com.atlassian.confluence.spaces.SpaceManager;
import com.atlassian.confluence.util.ajax.AjaxResponse;

import com.dirox.confluence.labelmanagement.service.LabelManagementService;
import static com.google.common.base.Preconditions.checkNotNull;

import org.apache.commons.lang.StringUtils;

import org.apache.log4j.Logger;

/**
 * DeleteLabelAjaxAction extends ConfluenceActionSupport implements Beanable
 * <p>
 * This class implements delete label action.
 */
public class DeleteLabelAjaxAction extends ConfluenceActionSupport
    implements Beanable {
    private static final long serialVersionUID = 1L;
    private static Logger log = Logger.getLogger(DeleteLabelAjaxAction.class);
    private SpaceManager spaceManager;
    private String spaceKey;
    private Object bean;
    private String label;
    private LabelManagementService labelManagementService;

    public DeleteLabelAjaxAction() {
    }

    /**
     * This method execute the delete label action
     * @return the result of action in String (ERROR|SUCCESS)
     * @throws Exception
     */
    public String deleteLabel() throws Exception {
        if (!validateInput()) {
            return ERROR;
        }

        try {
            // Check if spaceKey is not blank then delete in this space,
            // else delete in all spaces.
            if (StringUtils.isNotBlank(spaceKey)) {
                labelManagementService.deleteLabelInSpace(spaceKey, label);
            } else {
                labelManagementService.deleteLabelInAllSpaces(label);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e.getCause());
            bean = AjaxResponse.failure(e.getMessage());

            return ERROR;
        }

        bean = AjaxResponse.success(getText("label-management.code.success"));

        return SUCCESS;
    }

    /**
     * Validate input for delete label action
     *
     * @return true if validate the input successfully, else return false
     */
    private boolean validateInput() {
        // check if the label is empty
        if (StringUtils.isBlank(label)) {
            bean = AjaxResponse.failure(getText(
                        "label-management.error.empty.label"));

            return false;
        }

        // check if the label does not exist
        if (labelManager.getLabel(label) == null) {
            bean = AjaxResponse.failure(getText(
                        "label-management.error.not.exist.label"));

            return false;
        }

        // check if the space does not exist
        if (StringUtils.isNotBlank(spaceKey) &&
                (spaceManager.getSpace(spaceKey) == null)) {
            bean = AjaxResponse.failure(getText(
                        "label-management.error.not.exist.space"));

            return false;
        }

        return true;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public void setSpaceKey(String spaceKey) {
        this.spaceKey = spaceKey;
    }

    public String getLabel() {
        return label;
    }

    public String getSpaceKey() {
        return spaceKey;
    }

    public Object getBean() {
        return bean;
    }

    public void setSpaceManager(SpaceManager spaceManager) {
        this.spaceManager = checkNotNull(spaceManager);
    }

    public void setLabelManagementService(
        LabelManagementService labelManagementService) {
        this.labelManagementService = checkNotNull(labelManagementService);
    }
}
